package br.consultorio.digital.dao;

import java.util.ArrayList;

import android.app.Activity;
import br.consultorio.digital.modelo.entidade.Paciente;

public class DAOPaciente extends DAO {

	// private Activity activityAtividadeData;
	private static DAOPaciente instance;

	public static DAOPaciente getInstance(Activity context) {
		if (instance == null) {
			synchronized (DAOPaciente.class) {

				if (instance == null) {
					instance = new DAOPaciente();
					abrecriaBanco(context);
				}
			}
		}
		return instance;
	}

	public ArrayList<Object> consultarNm_Paciente(String paciente) {
		setCursor(getDatabase()
				.query("tb_paciente as p",
						new String[] { "distinct p.id_paciente", "p.nm_paciente" },
						"a.nm_paciente like '%" + paciente + "%'",
						null,
						null,
						null,
						"a.nm_paciente"));

		ArrayList<Object> ap = new ArrayList<Object>();

		while (getCursor().moveToNext()) {
			ap.add(setPaciente());
		}

		fechaBanco();
		return ap;
	}

	public ArrayList<Object> consultarTodos() {
		setCursor(getDatabase()
				.query("tb_paciente as p",
						new String[] { "distinct p.ID_Paciente", "p.NM_Paciente" },
						null,
						null,
						null,
						null,
						"a.NM_Paciente"));

		ArrayList<Object> ap = new ArrayList<Object>();

		while (getCursor().moveToNext()) {
			ap.add(setPaciente());
		}

		fechaBanco();
		return ap;
	}
	
	private Paciente setPaciente() {
		Paciente p = new Paciente();
		p.setId_paciente(getCursor().getInt(
				getCursor().getColumnIndexOrThrow("ID_Paciente")));
		p.setNm_paciente(getCursor().getString(
				getCursor().getColumnIndexOrThrow("NM_Paciente")));
		return p;
	}
}
